package core.actions;

import java.util.Vector;
/**
 * 
 * @author Anton
 * Several actions to be put in a spinner
 */
public class ActionMap<E> {
	int depth=3;
	
	
	//first is circles / rows, second is pies / columns

	private Vector<Vector<E>> actions = new Vector<Vector<E>>();
	private E centerAction;
	
	public void addPie(Vector<E> pie){
		actions.add(pie);
	}
	public void add(E a){
		boolean foundSpot=false;
		for(Vector<E> v : actions){
			if(v.size()<depth){
				
				v.add(a);
				//System.out.println("Placed"+a.toString()+" at "+v.indexOf(a));
				foundSpot=true;
				break;
			}				
		}
		if(!foundSpot){
			Vector<E> tmp = new Vector<E>();
			tmp.add(a);
			addPie(tmp);
		}
	}
	/**
	 * replaces the action at the specified position with the new action.
	 * @param a
	 * @param row
	 * @param col
	 */
	public void set(E a, int row, int col){
		
		if(actions.get(row).size() > col)
			actions.get(row).set(col, a);
		else{
			//actions.get(row).setSize(col+1);
			//actions.get(row).set(col, a);
			centerAction = a;
		}
	}
	public E getAction(int a, int b){
		try{
			return actions.get(a).get(b);
		}
		catch(ArrayIndexOutOfBoundsException ex){
			System.out.println("Action "+a+", "+b+"not found");
			if(b!=0){
				if(centerAction != null)
					return centerAction;
				return getAction(a,0);
			}
			else
				return null;
		}		
	}
	public E getActionAcceptNull(int a, int b){
		try{
			return actions.get(a).get(b);
		}
		catch(ArrayIndexOutOfBoundsException ex){
			if(b>=actions.get(a).size())
				return centerAction;
			return null;
		}		
	}
	public Vector<E> getAllActions(){
		Vector<E> returnVector = new Vector<E>();
		for(Vector<E> subActions : actions){
			for(E a : subActions){
				returnVector.add(a);
			}
		}
		returnVector.add(centerAction);
		return returnVector;
	}
	public Vector<E> getActions(int pieId){
		return actions.get(pieId);
	}
	public int getDepth(){
		int m = 0;
		for(Vector v : actions){
			if(v.size()>m)
				m=v.size();
		}
		return m;
	}
	public int getRadius(){
		return actions.size();
	}
	public void clear(){
		actions.clear();
	}
	public String toString(){
		String s="";
		for(Vector<E> v : actions){
			for(E a : v){
				s=s+a.toString()+"\t";
			}
			s=s+"\n";
		}
		return s;
	}
	public void increaseRadius(){
		Vector<E> newPie=new Vector<E>();
		newPie.setSize(getDepth());
		addPie(newPie);
	}
	public void decreaseRadius(){
		actions.removeElement(actions.lastElement());
	}
	public void increaseDepth(){
		int newDepth=getDepth()+1;
		for(Vector<E> v : actions){
			v.setSize(newDepth);
			System.out.println("Depth: "+v.size());
		}
	}
	public void decreaseDepth(){
		int newDepth=getDepth()-1;
		for(Vector<E> v : actions){
			v.setSize(newDepth);
		}
	}
	public void setWantedDepth(int d){
		depth=d;
	}
	
	
}
